package model;

import extras.Extra1Activity;
import extras.Extra2Activity;
import extras.Extra3Activity;
import extras.Extra4Activity;
import extras.Extra5Activity;
import touche.touche.GameActivity;
import touche.touche.R;
import android.app.Activity;
import android.content.Intent;

public class GameManager {
	private static GameManager _gameManagerInstance;

	private static int _levelCount = 6;

	public static GameManager getInstance() {
		if (_gameManagerInstance == null) {
			_gameManagerInstance = new GameManager();
		}
		return _gameManagerInstance;
	}

	private LevelModel[] _levels = new LevelModel[_levelCount];

	private int _score;
	private int _life;

	public int getLife() {
		return _life;
	}

	public void setLife(int life) {
		this._life = life;
	}

	private int _currentLevelIndex;

	private Activity _activity = null;

	public Activity getActivity() {
		return _activity;
	}

	private GameManager() {
		Integer[] pictures = { R.drawable.hand_blue, R.drawable.hand_red };
		int durationLevel = 30000;
		_levels[0] = new LevelModel("Niveau 1", false, false, durationLevel, 1600,
				pictures, 32, 5, 5, R.string.help_level1, R.string.start_game);

		_levels[1] = new LevelModel("Niveau 2", true, false, durationLevel, 1000,
				pictures, 32, 15, 5, R.string.help_level2, R.string.new_level);

		_levels[2] = new LevelModel("Niveau 3", true, false, durationLevel, 800,
				pictures, 32, 20, 5, R.string.help_level3, R.string.new_level);

		_levels[3] = new LevelModel("Niveau 4", true, true, durationLevel, 600,
				pictures, 32, 25, 5, R.string.help_level4, R.string.new_level);

		_levels[4] = new LevelModel("Niveau 5", true, true, durationLevel, 400,
				pictures, 32, 30, 5, R.string.help_level5, R.string.new_level);

		init();
	}

	public void start(Activity activity) {
		if (null != this._activity) {
			this._activity.finish();
		}
		if(1 == _currentLevelIndex && _score >= 100) {
			activity.startActivity(new Intent(activity, Extra1Activity.class));
		} else if(2 == _currentLevelIndex && _score >= 200) {
			activity.startActivity(new Intent(activity, Extra2Activity.class));
		}  else if(3 == _currentLevelIndex && _score >= 500) {
			activity.startActivity(new Intent(activity, Extra3Activity.class));
		}  else if(4 == _currentLevelIndex && _score >= 700) {
			activity.startActivity(new Intent(activity, Extra4Activity.class));
		}  else if(5 == _currentLevelIndex && _score >= 900) {
			activity.startActivity(new Intent(activity, Extra5Activity.class));
		}  else {
			activity.startActivity(new Intent(activity, GameActivity.class));
		}
	}

	public LevelModel getCurrentLevel() {
		return _levels[_currentLevelIndex];
	}

	public int getCurrentLevelIndex() {
		return _currentLevelIndex;
	}

	public int getScore() {
		return _score;
	}

	public void init() {
		if (null != _activity) {
			_activity.finish();
		}
		_score = 0;
		_life = 100;
		_currentLevelIndex = 0;
	}

	public boolean setNextLevelIndex() {
		if (_currentLevelIndex < _levelCount) {
			this._currentLevelIndex++;
			return true;
		}
		return false;
	}

	public void setScore(int score) {
		this._score = score;
	}

	public void setActivity(Activity activity) {
		this._activity = activity;
	}
}
